package com.example.puback.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @version 1.0.0
 * @className: ConfirmConfig
 * @description: 发布确认兜底方案
 * @author: LiJunYi
 * @create: 2022/8/4 14:54
 */
@Configuration
public class ConfirmConfig
{
    public static final String CONFIRM_EXCHANGE_NAME = "confirm-exchange";
    public static final String CONFIRM_QUEUE_NAME = "confirm-queue";
    public static final String CONFIRM_ROUTING_KEY = "confirm-key";

    /**
     * 备份交换机
     */
    private static final String BACKUP_EXCHANGE_NAME = "backup-exchange";

    /**
     * 备份队列名称
     */
    public static final String BACKUP_QUEUE_NAME = "backup-queue";

    /**
     * 警告队列名称
     */
    public static final String WARNING_QUEUE_NAME = "warning-queue";

    /**
     * 声明交换机
     *
     * @return {@link DirectExchange}
     */
    @Bean("confirmExchange")
    public DirectExchange directExchange(){
        // 确认交换机配置备份交换机 以确保宕机后将消息转发到备份交换机
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME).durable(true)
                .withArgument("alternate-exchange", BACKUP_EXCHANGE_NAME).build();
    }

    /**
     * 声明队列
     *
     * @return {@link Queue}
     */
    @Bean("confirmQueue")
    public Queue confirmQueue(){
        HashMap<String, Object> map = new HashMap<>(8);
        return new Queue(CONFIRM_QUEUE_NAME,false,false,false,map);
    }

    /**
     * 队列确认绑定交换
     *
     * @param queue    队列
     * @param exchange 交换
     * @return {@link Binding}
     */
    @Bean
    public Binding queueConfirmBindingExchange(@Qualifier("confirmQueue")Queue queue,
                                               @Qualifier("confirmExchange")Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_ROUTING_KEY).noargs();
    }

    /**
     * 备份交换机的创建
     *
     * @return {@link FanoutExchange}
     */
    @Bean("backupExchange")
    public FanoutExchange backupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    /**
     * 备份队列
     *
     * @return {@link Queue}
     */
    @Bean("backupQueue")
    public Queue backupQueue(){
        HashMap<String, Object> map = new HashMap<>(8);
        return new Queue(BACKUP_QUEUE_NAME,false,false,false,map);
    }

    /**
     * 警告队列
     *
     * @return {@link Queue}
     */
    @Bean("warningQueue")
    public Queue warningQueue(){
        HashMap<String, Object> map = new HashMap<>(8);
        return new Queue(WARNING_QUEUE_NAME,false,false,false,map);
    }

    /**
     * 备份队列绑定交换机
     *
     * @param queue    队列
     * @param exchange 交换
     * @return {@link Binding}
     */
    @Bean
    public Binding backupConfirmBindingExchange(@Qualifier("backupQueue")Queue queue,
                                                @Qualifier("backupExchange")FanoutExchange exchange){

        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * 警告队列绑定交换机
     *
     * @param queue    队列
     * @param exchange 交换
     * @return {@link Binding}
     */
    @Bean
    public Binding warningConfirmBindingExchange(@Qualifier("warningQueue")Queue queue,
                                                 @Qualifier("backupExchange")FanoutExchange exchange){

        return BindingBuilder.bind(queue).to(exchange);
    }
}
